﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using YaAppoint.Rules;

namespace YaAppoint.Web.Controllers
{
    public class RuleController(IRuleAppService ruleAppService) : AbpController
    {
        private readonly IRuleAppService _ruleAppService = ruleAppService;

        /// <summary>
        /// 加载规则组选项
        /// </summary>
        [HttpGet("/api/rule/get-rule-group-options")]
        public async Task<IActionResult> GetRuleGroupOptions(RuleLevel level,Guid? depId,Guid?deviceTypeId,Guid? deviceId)
        {
            return new JsonResult(await _ruleAppService.LoadRuleGroupOptions(level,depId,deviceTypeId,deviceId));
        }

        /// <summary>
        /// 获取规则详情
        /// </summary>
        [HttpGet("/api/rule/get-rule-detail")]
        public async Task<IActionResult> GetRuleDetail(Guid id)
        {
            return new JsonResult(await _ruleAppService.GetRuleDetail(id));
        }

        /// <summary>
        /// 获取规则组详情
        /// </summary>
        [HttpGet("/api/rule/get-rule-group-detail")]
        public async Task<IActionResult> GetRuleGroupDetail(Guid id)
        {
            return new JsonResult(await _ruleAppService.GetRuleGroupDetail(id));
        }

        /// <summary>
        /// 加载表格数据
        /// </summary>
        [HttpGet("/api/rule/get-table-data")]
        public async Task<IActionResult> LoadTableData(RuleLevel level, Guid? depId, Guid? deviceTypeId, Guid? deviceId)
        {
            return new JsonResult(await _ruleAppService.LoadTableData(level, depId, deviceTypeId, deviceId));
        }

        /// <summary>
        /// 获取指定规则组规则列表
        /// </summary>
        [HttpGet("/api/rule/get-group-rules")]
        public async Task<IActionResult> GetGroupRules(Guid groupId)
        {
            return new JsonResult(await _ruleAppService.GetGroupRules(groupId));
        }

        /// <summary>
        /// 提交新建规则组
        /// </summary>
        [HttpPost("/api/rule/rule-group-create-submit")]
        public async Task<IActionResult> RuleGroupCreateSubmit([FromBody]RuleGroupCreateDto dto)
        {
            return new JsonResult(await _ruleAppService.RuleGroupCreateSubmit(dto));
        }

        /// <summary>
        /// 提交修改规则组
        /// </summary>
        [HttpPost("/api/rule/rule-group-edit-submit")]
        public async Task<IActionResult> RuleGroupEditSubmit([FromBody] RuleGroupDto dto)
        {
            return new JsonResult(await _ruleAppService.RuleGroupEditSubmit(dto));
        }

        /// <summary>
        /// 删除规则组
        /// </summary>
        [HttpGet("/api/rule/delete-rule-goup")]
        public async Task<IActionResult> DeleteRuleGroup(Guid id)
        {
            return new JsonResult(await _ruleAppService.DeleteRuleGroup(id));
        }

        /// <summary>
        /// 提交新建规则
        /// </summary>
        [HttpPost("/api/rule/rule-create-submit")]
        public async Task<IActionResult> RuleCreateSubmit([FromBody] RuleCreateDto dto)
        {
            return new JsonResult(await _ruleAppService.RuleCreateSubmit(dto));
        }

        /// <summary>
        /// 提交规则修改
        /// </summary>
        [HttpPost("/api/rule/rule-edit-submit")]
        public async Task<IActionResult> RuleEditSubmit([FromBody] RuleDto dto)
        {
            return new JsonResult(await _ruleAppService.RuleEditSubmit(dto));
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        [HttpGet("/api/rule/delete-rule")]
        public async Task<IActionResult> DeleteRule(Guid id)
        {
            return new JsonResult(await _ruleAppService.DeleteRule(id));
        }

        /// <summary>
        /// 获取逻辑类型选项
        /// </summary>
        [HttpGet("/api/rule/get-logic-type-options")]
        public IActionResult GetLogicTypeOptions()
        {
            return new JsonResult(_ruleAppService.GetLogicTypeOptions());
        }

        /// <summary>
        /// 获取层级选项
        /// </summary>
        [HttpGet("/api/rule/get-level-options")]
        public IActionResult GetLevelOptions()
        {
            return new JsonResult(_ruleAppService.GetLevelOptions());
        }
    }
}
